﻿using Infrastructure.Web;
using MyData;
using MyData.ApiDto;
using MyData.Data.Repository;
using MyData.Models;
using MyDomain;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyRepository
{
    public class SyncRLRepository : RepositoryBase<SyncRL>, ISyncRLRepository
    {
        public SynsOput GetSynsOput(Guid mobileId, int pageIndex = 0, int pageSize = 10)
        {
            SynsOput synsOput = new SynsOput();
            using (var db = this.NewDB())
            {
                var syns = db.Query<SyncRL>().Where(c => c.MobileId == mobileId).ToList();
                if (syns.Count == 0)  return synsOput;

                var users = db.Query<UserInfo>().Where(c=>c.NeedConfirm==0).ToList();
                var departs = db.Query<Depart>().ToList();
                var visitors = db.Query<Visitor>().Where(c=>c.NeedConfirm==0).ToList();
                synsOput.DeleteUserIds = syns.Where(c => c.OperateType == (int)EnumOperateType.删除 && c.UserType == (int)EnumUserType.员工).Select(c => c.UserId).ToList();
                var list = (from s in syns
                            where s.UserType == (int)EnumUserType.员工 && s.OperateType == (int)EnumOperateType.新增
                            join u in users on s.UserId equals u.Id
                            join d in departs on u.DepartId equals d.Id
                            select new UserInfoSynsOput()
                            {
                                UserId = u.Id,
                                DepartName = d.Name,
                                NickName = u.NickName,
                                HeadUrl = u.HeadUrl
                            }).ToList();
                synsOput.TotolPage = (int)Math.Ceiling((double)list.Count / pageSize);
                if (pageIndex != 0)
                {
                    list = list.Skip(pageSize * (pageIndex - 1)).Take(pageSize).ToList();
                }
                synsOput.UserInfoSynsOputs = list;
                synsOput.DeleteVisitors = syns.Where(c => c.OperateType == (int)EnumOperateType.删除 && c.UserType == (int)EnumUserType.访客).Select(c => c.UserId).ToList();
                synsOput.VisitorSynsOputs = (from s in syns
                                             where s.UserType == (int)EnumUserType.访客 && s.OperateType == (int)EnumOperateType.新增
                                             join v in visitors on s.UserId equals v.Id
                                             select new VisitorSynsOput()
                                             {
                                                 VisitorId = v.Id,
                                                 Company = v.Company,
                                                 Name = v.Name,
                                                 HeadUrl = v.HeadUrl,
                                                 EndTime = v.EndDate
                                             }).ToList();

                return synsOput;

            }
        }

        public bool DeleteSyns(Guid mobileId)
        {
            using (var db = this.NewDB())
            {
                return db.Delete<SyncRL>(c => c.MobileId == mobileId) > 0;
            }
        }

        public List<SyncRL> GetSynsUsers(Guid mobileId)
        {
            using (var db = this.NewDB())
            {
                return db.Query<SyncRL>().Where(c => c.MobileId == mobileId && c.UserType == 0).ToList();
            }
        }
        public bool DeleteSynsById(Guid syncId)
        {
            using (var db = this.NewDB())
            {
                db.DeleteByKey<SyncRL>(syncId);
                return true;
            }
        }
        public SyncRL GetSynsById(Guid syncId)
        {
            using (var db = this.NewDB())
            {
                return db.QueryByKey<SyncRL>(syncId);
            }
        }
    }
}
